<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class Shop extends Model implements Sortable
{
    use SortableTrait;

    public $sortable = [
        'order_column_name' => 'sort',
        'sort_when_creating' => true,
    ];

    protected $primaryKey = 'id';

    protected $fillable = [
        'name',                                 #店名
        'user_id',                              #用户id
        'face_picture',                         #店铺图片
        'begin_business_hours',                 #开始营业时间
        'end_business_hours',                   #结束营业时间
        'begin_free_time',                      #开始空闲时间
        'end_free_time',                        #结束空闲时间
        'status',
        'sort',
        'recommended',
    ];

    const PADDING = 'padding';

    const PASS = 'pass';

    const FAIL = 'fail';

    static $STATUS_MAP = [
        self::PADDING => '待審核',
        self::PASS => '通過',
        self::FAIL => '不通过',
    ];

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id')->withDefault();
    }

    public function data()
    {
        return $this->belongsTo(UserData::class, 'user_id', 'user_id')->withDefault();
    }

    public function products()
    {
        return $this->hasMany(Product::class, 'shop_id', 'id');
    }

    public function packages()
    {
        return $this->hasMany(Package::class, 'shop_id', 'id');
    }

    public function product_orders()
    {
        return $this->hasManyThrough(Order::class, Product::class, 'shop_id', 'goods_id')->where('goods_type', 'App\Models\Product');
    }

}
